/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaassist;

import fw.anotations.AnnotationsUtil;
import fw.anotations.AnotacionAtom;
import fw.anotations.ClassAnnotationInfo;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.Loader;
import javassist.NotFoundException;
import javaassist.Hello;
import javassist.CtField;
import javassist.CtNewMethod;
import testes.MiBean;

/**
 *
 * @author HP_Propietario
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        //createInterface();
        ClassAnnotationInfo classAnnotationInfo=AnnotationsUtil.getClassAnnotation(MiBean.class);
         // classAnnotationInfo.getAnnotation().put("miclase", new AnotacionAtom());
    }

    public static void addNewObject() throws Exception {
        ClassPool pool = ClassPool.getDefault();
        CtClass ctClass = pool.get("javaassist.Hello");
        CtMethod ctMethod = ctClass.getDeclaredMethod("say");
        ctMethod.insertBefore("{ System.out.println($1); }");
        Class clzz = ctClass.toClass();

        Hello hello = (Hello) clzz.newInstance();
        hello.say("nombresss");
    }

    public static void addNewMethod() throws CannotCompileException, InstantiationException, IllegalAccessException {
        ClassPool classPool = ClassPool.getDefault();
        CtClass ctClass = classPool.makeClass("Hola1");
        CtMethod ctMethod = CtNewMethod.make("public int xmove(int dx, int x) { return  x += dx; }", ctClass);
        ctClass.addMethod(ctMethod);
        Object object = ctClass.toClass().newInstance();
    }

    public static void addField() throws CannotCompileException {
        ClassPool pool = ClassPool.getDefault();
        CtClass ctClass = pool.makeClass("Hola1");
        CtField ctField = CtField.make("public int micampo", ctClass);
        ctClass.addField(ctField);
    }

    public static void getAnnotations() throws NotFoundException, ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
        ClassPool classPool = ClassPool.getDefault();
        CtClass anotationClass = classPool.get("testes.MiBean");
        
        /*anotaciones para los metodos*/
        CtMethod[] ctMethods = anotationClass.getMethods();
        for (int i = 0; i < ctMethods.length; i++) {
            CtMethod ctMethod = ctMethods[i];
            Object[] objects = ctMethod.getAnnotations();
            for (int j = 0; j < objects.length; j++) {
                Annotation anotacionMethod = (Annotation) objects[j];
                System.out.println("--->" + anotacionMethod.annotationType().getName());


            }

        }
        /*anotaciones para las  clases*/
        Object[] annotations = anotationClass.getAvailableAnnotations();
        for (int i = 0; i < annotations.length; i++) {
            Annotation annotation = (Annotation) annotations[i];
            System.out.println("--->" + annotation.annotationType().getName());
            Method[] methods = annotation.annotationType().getDeclaredMethods();
            System.out.println("---------->fields para \t" + annotation.annotationType().getName());
            for (int j = 0; j < methods.length; j++) {
                Method method = methods[j];
                System.out.println("metodos---> " + method.getName());
                Object[] objects=new Object[]{null};
                String string = (String) method.invoke(annotation, null);
                System.out.println("valor---> " + string);
            }
            System.out.println("fin clases anno");

        }



        /*anotaciones para las  Field*/
        CtField[] ctField = anotationClass.getDeclaredFields();

        for (int i = 0; i < ctField.length; i++) {
            CtField ctField1 = ctField[i];

            Object[] objects = ctField1.getAnnotations();
            for (int j = 0; j < objects.length; j++) {
                Annotation annotationField = (Annotation) objects[j];
                System.out.println("--->" + annotationField.annotationType().getName());
            }

        }



    }

    public static Class createInterface() throws CannotCompileException {
        ClassPool pool = ClassPool.getDefault();
        CtClass ctinterface = pool.makeInterface("MiBean" + "MBean");
        CtMethod ctMethodInterface = CtMethod.make("public void ope1();", ctinterface);
        ctinterface.addMethod(ctMethodInterface);
        return ctinterface.toClass();
    }

    public static void createMethod() {
    }
}
